// config

var app = angular.module('app', ['ui.load', 'ui.router']);

angular.module('app')
  .config(['$controllerProvider', '$compileProvider', '$filterProvider', '$provide',
	    function ($controllerProvider,   $compileProvider,   $filterProvider,   $provide) {
	        // lazy controller, directive and service
	        app.controller = $controllerProvider.register;
	        app.directive  = $compileProvider.directive;
	        app.filter     = $filterProvider.register;
	        app.factory    = $provide.factory;
	        app.service    = $provide.service;
	        app.constant   = $provide.constant;
	        app.value      = $provide.value;
	    }
	])
	.factory('myHttpInterceptor',function($location){
		$.ajaxSetup({
			type : "POST",
			error : function(jqXHR, textStatus, errorThrown) {
				switch (jqXHR.status) {
				case (500):
					alert("服务器系统内部错误");
					break;
				case (401): // 未登录
					var c = $location.url();
	                $location.url('/access/login');
	                $location.replace();
					break;
				case (403):
					alert("无权限执行此操作");
					break;
				case (408):
					alert("请求超时");
					break;
				default:
					throw errorThrown;
				}
			}
		});
	    return {
	        request:function(config){
	            //console.log("request===",config);
	            return  config;
	        },
	        'requestError': function(rejection) {
	            //console.log("requestError===",rejection);
	        },
	        'response': function(response) {
	            //console.log("response===",response);
	            // 401 用户未登录
	            if(response.status == 401) {
	                var c = $location.url();
	                $location.url('/access/login');
	                $location.replace();
	            }else{
	                return response;
	            }
	        },
	        'responseError': function(response) {
	            if(response.status == 401) {
	                var c = $location.url();
	                $location.url('/access/login');
	                $location.replace();
	            }else{
	                //alert(response.statusText +" :"+ response.status);
	                return false;
	            }
	        }
	    };
	})
	.config(['$httpProvider', function($httpProvider) {
	    $httpProvider.interceptors.push('myHttpInterceptor');
	}])
	.config(function(){
        jQuery.validator.setDefaults({
            errorClass: 'help-block animation-slideDown', // You can change the animation class for a different entrance animation - check animations page
            errorElement: 'div',
            errorPlacement: function(error, e) {
                var eleErrContains = e.parents('.tdgroup');
                if(eleErrContains.length == 0){
                    eleErrContains = e.parents('.form-group > div');
                }
                eleErrContains.append(error);
            },
            highlight: function(e) {
                $(e).closest('.form-group').removeClass('has-success has-error').addClass('has-error');
                $(e).closest('.help-block').remove();
            },
            success: function(e) {
                e.closest('.form-group').removeClass('has-success has-error');
                e.closest('.help-block').remove();
            }
        });
    });
window.APP = { version : 'v=201605' };